<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ajax extends CI_Controller
{
	public function __construct()
	{
        parent::__construct();
		if(! is_ajax())
		{
			redirect(BASE_URL);
		}
		$member = $this->session->userdata('user');
		$this->memberid=$member['userid'];
		$this->memberemail=$member['email'];
    }
	public function index()
	{
		echo "hello";
	}
	public function send_mail($to,$subject,$message)
	{
		$settingdata=$this->general_model->select_val("setting","*","id = 1");
		/*$config=Array(
			"protocol"=>"smtp",
			"smtp_host"=>"ssl://smtp.gmail.com",
			"smtp_port"=>465,
			"smtp_user"=>"niranjannsahoo@gmail.com",
			"smtp_pass"=>"paikarapursibu"
		);
		$this->load->library('email',$config);*/
		$this->load->library('email');
		$this->email->set_mailtype("html");
		$this->email->set_newline("\r\n");
		$this->email->from($settingdata['notification_email'],$settingdata['site_name']);
		$this->email->reply_to($settingdata['notification_email'], $settingdata['site_name']);	
		$this->email->to($to);		
		$this->email->subject($subject);		
		$this->email->message($message);
		$this->email->send();
	}
	public function state($countryid) {
		
		$json = array();
			$json = array(
				'country_id'        => $countryid,
				'zone'           => $this->general_model->select("zone","*","country_id ='".$countryid."'")		
			);
		echo json_encode($json);
	}
	public function onlineuser()
	{
		$json = array();
		$users=get_users_online();
		//$json=array("users"=>$users);
		/*foreach($users as $key=>$value)
		{
			$json['users'][]=$this->general_model->select_val("zap_member","userid,firstname,photo","userid ='".$key."'")	;	
		}*/
		$myfriends=$this->general_model->get_jointable("zap_friends","zap_member","*","zap_friends.friend_id=zap_member.userid","zap_friends.userid = ".$this->memberid . " and zap_member.activated = 1");
			//print_r($myfriends);
		foreach($myfriends as $friend)
		{
			if (array_key_exists($friend['userid'], $users))
			{
				$json['users'][]=array(
								"userid"=>$friend['userid'],
								"firstname"=>$friend['firstname'],
								"photo"=>$friend['photo'],
								"online"=>1);
			}
			else
			{
				$json['users'][]=array(
								"userid"=>$friend['userid'],
								"firstname"=>$friend['firstname'],
								"photo"=>$friend['photo'],
								"online"=>0);
			}
		}
		echo json_encode($json);
	}
	public function checkusername()
	{
		$username = $_POST['username'];
		$count=$this->general_model->count_row('zap_member','username ='."'".$username."'");
		
		if ( preg_match('/\s/',$username) )
		{
			echo '<font color="#cc0000"><STRONG>Username</STRONG> does not allow Space.</font>';
		
		}
		
		else if($count)
		{
			echo '<font color="#cc0000"><STRONG>'.$username.'</STRONG> is already in use.</font>';
		}
		else
		{
			echo 'OK';
		}
	}
	public function friend_request()
	{
		$userid=$_POST['userid'];
		$action=$_POST['action'];
		if($action=="add")
		{
			$requestdata=array(
					"request_by"=>$this->memberid,
					"request_to"=>$userid,
					"status"=>0,
					"date_added"=>date("Y-m-d")
			);
			$requestid=$this->general_model->insert('zap_friend_request',$requestdata);
			if($requestid)
			{
				echo "requested";
			}
		}
		else if($action=="cancel")
		{
			
			$this->general_model->delete_row('zap_friend_request',"request_by = ".$this->memberid ." and request_to = ".$userid);
			
			echo "cancel";
			
		}
		else if($action=="confirm")
		{
			
			$this->general_model->delete_row('zap_friend_request',"request_to = ".$this->memberid ." and request_by = ".$userid);
			$confirmdata=array(
					"userid"=>$this->memberid,
					"friend_id"=>$userid,
					"status"=>1,
					"date_added"=>date("Y-m-d")
			);
			$confirmredata=array(
					"userid"=>$userid,
					"friend_id"=>$this->memberid,
					"status"=>1,
					"date_added"=>date("Y-m-d")
			);
			$confirmid=$this->general_model->insert('zap_friends',$confirmdata);
			$confirmreid=$this->general_model->insert('zap_friends',$confirmredata);
			
			if($confirmid)
			echo "confirm";
			
		}
		
	}
	public function addcomment()
	{
		$postid=$_POST['postid'];
		$comment=$_POST['comment'];
		$userid=$this->memberid;
		$postdetails=$this->general_model->select_val("zap_post","userid","postid =".$postid);
		$comment_date=date("Y-m-d H:m:s");
		$zapmember=$this->general_model->get_join2table("zap_member","users","*","zap_member.userid=users.userid","zap_member.userid = ".$this->memberid);
		$imgsrc=($zapmember['photo'] == "" ? DEFAULT_IMG : $zapmember['photo']);
		$commentdata=array("postid"=>$postid,
							"post_userid"=>$postdetails['userid'],
							"userid"=>$userid,
							"comments"=>$comment,
							"comment_date"=>$comment_date,
							"status"=>1);
		$commentid=$this->general_model->insert('zap_postcomment',$commentdata);
		
		if($commentid)
		{
			$html='<div class="media">';
			$html.='<a class="pull-left" href="#">';
			$html.='<img src="'.base_url('assets/uploads/.thumbs/images/zap').'/'.$imgsrc.'" alt="Profile-img" class="media-object" height="32" width="32">';				
			$html.='</a>';
			$html.='<div class="media-body">';
			$html.='<h5 class="media-heading text-primary">'.$zapmember['firstname'].'</h5>';
			$html.='<p>'.$comment.'</p>';
			$html.='<p class="text-muted">'. date('m/d/Y h:i A', strtotime($comment_date)) .'</p>';
			$html.='</div>';
			$html.='</div>';
			echo $html;			
		}
		
	}
	public function addphotocomment()
	{
		$postid=$_POST['postid'];
		$comment=$_POST['comment'];
		$userid=$this->memberid;
		$comment_date=date("Y-m-d H:m:s");
		$zapmember=$this->general_model->get_join2table("zap_member","users","*","zap_member.userid=users.userid","zap_member.userid = ".$this->memberid);
		$imgsrc=($zapmember['photo'] == "" ? DEFAULT_IMG : $zapmember['photo']);
		$commentdata=array("postid"=>$postid,
							"userid"=>$userid,
							"comments"=>$comment,
							"comment_date"=>$comment_date,
							"status"=>1);
		$commentid=$this->general_model->insert('zap_postcomment',$commentdata);
		
		if($commentid)
		{
			$html='<div class="media">';
			$html.='<a class="pull-left" href="#">';
			$html.='<img src="'.base_url('assets/uploads/.thumbs/images/zap').'/'.$imgsrc.'" alt="Profile-img" class="media-object" height="32" width="32">';				
			$html.='</a>';
			$html.='<div class="media-body">';
			$html.='<h5 class="media-heading text-primary">'.$zapmember['firstname'].'<p class="text-muted pull-right">'.$comment_date.'</p></h5>';
			$html.='<p>'.$comment.'</p>';
			$html.='</div>';
			$html.='</div>';

			echo $html;			
		}
		
	}
	public function removecomment($commentid)
	{
		$this->general_model->delete_row('zap_post',"id = ".$commentid);
		echo "deleted";
	}
	public function addshare()
	{
		$facebook= $this->input->post('facebook');
		
		$twitter= $this->input->post('twitter');
		$instagram= $this->input->post('instagram');
		$youtube= $this->input->post('youtube');
		$snapchat= $this->input->post('snapchat');
		$tumblr= $this->input->post('tumblr');
		$zap= $this->input->post('zap');
		
		$data['media']=base_url().'assets/uploads/media/'.$this->input->post('media');
		$data['mtype']=$this->input->post('media_type');;
		$data['type']='';
		
		$social_user='zap';
		if($facebook)
		$social_user=$facebook;
		if($twitter)
		$social_user.=','.$twitter;
		if($instagram)
		$social_user.=','.$instagram;
		if($youtube)
		$social_user.=','.$youtube;
		if($snapchat)
		$social_user.=','.$snapchat;
		if($tumblr)
		$social_user.=','.$tumblr;
		if($zap)
		{
		$sharedata=array(
				"postid"=>$this->input->post('postid'),
				"userid"=>$this->memberid,	
				"share_comment"=>$this->input->post('share_comment'),
				"social_user"=>$social_user,
				"date_added"=>date('Y-m-d H:i:s'),
				"status"=>1
		);
		$shareid=$this->general_model->insert('zap_postshare',$sharedata);
		
		$messagedata=array(
					"postid"=>$this->input->post('postid'),
					"userid"=>$this->memberid,
					"post_desc"=>$this->input->post('post_desc'),
					"social_user"=>$this->input->post('social_user'),
					"media"=>$this->input->post('media'),
					"media_type"=>$this->input->post('media_type'),
					"shareid"=>$shareid,
					"date_added"=>date('Y-m-d H:i:s'),
					"status"=>1);
		$postid=$this->general_model->insert('zap_post',$messagedata);
		}
		try
		{
			if($facebook)
			{
				//$adapter = $this->hybridauthlib->authenticate("Facebook");
				$adapter = $this->hybridauthlib->getAdapter($facebook);
				$adapter->setUserStatus(
						array(
							"message" => $this->input->post('share_comment'),
							'source'=>$data['media'],
							)
							
						);  
			}
			if($twitter)
			{
				//$adapter = $this->hybridauthlib->authenticate("Facebook");
				$adapter = $this->hybridauthlib->getAdapter($twitter);
				//$adapter->setUserStatus($this->input->post('message'));  
				$adapter->setUserStatus( "Hi there! this is just a random update to test some stuff" );

			}
			if($tumblr)
			{
				//$adapter = $this->hybridauthlib->authenticate("Facebook");
				$adapter = $this->hybridauthlib->getAdapter($tumblr);
				if($data['mtype']=="image") 
				$adapter->setUserStatusPhoto( $data['media']);
				else
				$adapter->setUserStatus($this->input->post('message')); 

			}
			if($snapchat)
			{
				//$adapter = $this->hybridauthlib->authenticate("Facebook");
				$adapter = $this->hybridauthlib->getAdapter($snapchat);
				
				
				if($data['mtype']=="image") 
				$adapter->setUserStatusPhoto($data['media']);
				

			}
			if($youtube)
			{
				//$adapter = $this->hybridauthlib->authenticate("Facebook");
				$adapter = $this->hybridauthlib->getAdapter($youtube);
				//print_r( $adapter->getAccessToken() );
				$adapter->direct_upload($data['media']);
				

			}
			
			//$this->session->set_flashdata('message', '<p class="success">Comments successfully Posted</p>');
			//redirect(BASE_URL.'users/postmedia');
		}
		catch( Exception $e )
		{
			// if code 8 => Provider does not support this feature
			if( $e->getCode() == 8 ){
				$data['message']= "<b style='color:red'>Error: Provider does not support this feature.</b> Currently only <b>Facebook, MySpace, Twitter, Identica and LinkedIn</b> do support this!
				<br />Please refer to the user guide to know more about each adapters capabilities. <a href='http://hybridauth.sourceforge.net/userguide.html'>http://hybridauth.sourceforge.net/userguide.html</a>";
			}
			else{
				$data['message']= "<b style='color:red'>Well, got an error:</b> " . $e->getMessage();
			} 
		}
		$this->session->set_flashdata('message', '<p class="success">Shared successfully Posted</p>');
			
		echo "success";
			
	}
	public function getcomments()
	{
		$postid=$_POST['postid'];
		$json['zapmember']=$this->general_model->select_val("zap_member","*","userid = ".$this->memberid);
		$json['comments']=$this->general_model->get_jointable("zap_member","zap_postcomment","*","zap_member.userid=zap_postcomment.userid","zap_postcomment.postid = ".$postid);
		echo json_encode($json);
	}
	public function deletelabel()
	{
		$labelid=$_POST['labelid'];
		$this->general_model->delete_row('zap_mailbox_label',"labelid = ".$labelid);
		$this->session->set_flashdata('message', '<p class="success">Label Deleted successfully</p>');
		
		echo "deleted";
	}
	public function movetolabel()
	{
		$labelid=$_POST['labelid'];
		
		$mailid=explode(',',$_POST['mailid']);
		$label=$this->general_model->select_val("zap_mailbox_label","*","labelid =".$labelid);
		if($labelid==0)
		$label_name="inbox";
		else
		$label_name=$label['label_name'];
		foreach($mailid as $id)
		{
			$updatedata=array("label_id"=>$labelid,
							"label"=>$label_name);
			$this->general_model->update("zap_mailbox",$updatedata,"mailid =".$id);
		}
		echo "moved";
	}
	public function reportuser()
	{
		$userid=$_POST['userid'];
		$zapmember=$this->general_model->select_val("zap_member","*","userid = ".$userid);
		
		$subject = "Zapout Abuse Reporting";
			
		$message  = "Dear Admin," . "\n\n";
		$message .= $zapmember['firstname'] ." is using abusive language or image.";
		$this->send_mail($this->memberemail,$subject,$message);
		$json['success']="Your report has been raised to admin";
		echo json_encode($json);
	}
	public function flaguser()
	{
		$userid=$_POST['userid'];
		$zapmember=$this->general_model->select_val("zap_member","*","userid = ".$userid);
		
		$subject = "Zapout Abuse Reporting";
			
		$message  = "Dear Admin," . "\n\n";
		$message .= $zapmember['firstname'] ." is using abusive image.";
		$this->send_mail($this->memberemail,$subject,$message);
		$json['success']="Your report has been raised to admin";
		echo json_encode($json);
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */